﻿using PhotoGallery.Data;
using PhotoGallery.Data.Entity;
using PhotoGallery.Data.Repository;
using PhotoGallery.Infrastructure;
using PhotoGallery.Models.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace PhotoGallery.Models
{
    public class PhotoInfo
    {
        public int ID { get; set; }
        [StringLength(25, ErrorMessageResourceType = typeof(ErrorMessages),
            ErrorMessageResourceName = "nameIsTooLong")]
        [LocalizedDisplayName("title", NameResourceType = typeof(Names))]
        public string Title { get; set; }
        public string FileName { get; set; }
        [DataType(DataType.MultilineText)]
        [LocalizedDisplayName("description", NameResourceType = typeof(Names))]
        public string Description { get; set; }
        [LocalizedDisplayName("date", NameResourceType = typeof(Names))]
        [DataType(DataType.Date)]
        public DateTime? UploadedOn { get; set; }
        // calculated
        public string Url { get; set; }
        public string UrlThumbnail { get; set; }
        public string FullFileName { get; set; }
        public string FullThumbnailName { get; set; }
    }
    public class PhotosModel : BaseModel
    {
        string imagePath, photoPath, url;
        PhotoRepository repository;
        AlbumInfo album;

        public PhotosModel(AlbumInfo Album)
        {
            album = Album;
            photoPath = BasePath + Settings.UploadsFolder + Owner + "\\Photos\\" + getAlbumFolder(album);
            imagePath = BasePath + Settings.UploadsFolder + Owner + "\\Images\\" + getAlbumFolder(album);
            url = Settings.UploadsUrl + Owner + "/Images/" + getAlbumUrl(album);

            var albumEntity = AlbumsModel.Convert(album);
            repository = new PhotoRepository(albumEntity, Owner);
        }

        //public PhotosModel(int albumID, bool loadAlbums = true)
        //{
        //    // TODO: album = new AlbumRepository(Owner).Get(albumID);
        //    photoPath = BasePath + Settings.UploadsFolder + Owner + "\\Photos\\" + getAlbumFolder(album);
        //    imagePath = BasePath + Settings.UploadsFolder + Owner + "\\Images\\" + getAlbumFolder(album); 
        //    url = Settings.UploadsUrl + Owner + "/Images/"+  getAlbumUrl(album);

        //    repository = new PhotoRepository(albumID, Owner);
        //}

        public PhotosModel(int albumID, string owner, string albumFolder)
        {
            repository = new PhotoRepository(albumID, owner, albumFolder);
            imagePath = BasePath + Settings.UploadsFolder + owner + "\\Images\\" + albumFolder +"\\";
            url = Settings.UploadsUrl + owner + "/Images/" + albumFolder + "/";
        }

        public IEnumerable<PhotoInfo> Get()
        {
            List<PhotoInfo> photos = new List<PhotoInfo>();
            var data = repository.Get();
            foreach (var entity in data)
            {
                var item = new PhotoInfo()
                {
                    ID = entity.ID,
                    Title = entity.Title,
                    FileName = entity.FileName,
                    Description = entity.Description,
                    UploadedOn = entity.UploadedOn,

                    Url = url + entity.FileName,
                    UrlThumbnail = url + "thump-" + entity.FileName,
                    FullFileName = imagePath + "large-" + entity.FileName,
                    FullThumbnailName = imagePath + "thump-" + entity.FileName
                };
                photos.Add(item);
            }
            return photos;
        }

        public PhotoInfo Get(int id)
        {
            var entity = repository.Get(id);
            var item = new PhotoInfo()
            {
                ID = entity.ID,
                Title = entity.Title,
                FileName = entity.FileName,
                Description = entity.Description,
                UploadedOn = entity.UploadedOn,
            
                Url = url + entity.FileName,
                UrlThumbnail = url + "thump-" + entity.FileName,
                FullFileName = imagePath + entity.FileName
            };

            return item;
        }

        public void Create(PhotoInfo photo, HttpPostedFileBase file)
        {
            if (file == null || file.FileName == string.Empty)
            {
                return;
            }
            // origin
            photo.FileName = FileUtils.CheckFileExists(file, imagePath);
            file.SaveAs(photoPath + photo.FileName);
            // thumbnail
            string resizedFileName = "thump-" + photo.FileName;
            FileHelper.SaveResizedImage(photoPath + photo.FileName,imagePath + resizedFileName);
            // large image
            resizedFileName = "large-" + photo.FileName;
            FileHelper.CreateResizedImage(photoPath + photo.FileName, imagePath + resizedFileName, 960, 720); 

            var entity = new Photo()
            {
                ID = photo.ID,
                Title = photo.Title,
                Description = photo.Description,
                FileName = photo.FileName,
                //CreateDate = photo..CreateDate,
            };
            repository.Create(entity);
        }

        public void Update(PhotoInfo photo, HttpPostedFileBase file)
        {
            if (file != null)
            {
                photo.FileName = FileUtils.CheckFileExists(file, imagePath);
                file.SaveAs(imagePath + photo.FileName);
            }
            var entity = Convert(photo);
            repository.Update(entity);
        }

        public void Delete(PhotoInfo photo)
        {
            var entity = Convert(photo);
            repository.Delete(entity);

        }

        private static Photo Convert(PhotoInfo photo)
        {
            var entity = new Photo()
            {
                ID = photo.ID,
                Title = photo.Title,
                Description = photo.Description,
                FileName = photo.FileName
            };
            return entity;
        }

        public void Erase(PhotoInfo photo)
        {
            repository.Erise(photo.ID);
            if (photo.FileName != string.Empty && System.IO.File.Exists(imagePath + photo.FileName))
            {
                System.IO.File.Delete(imagePath + photo.FileName);
                System.IO.File.Delete(imagePath + "thump-" + photo.FileName);
            }
        }
    }
}